package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.qo.BpmnInfoQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    //部署
    @RequiresPermissions("business:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deploy() {
        return prefix + "deployPage";
    }


    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQueryObject qo) {
        return bpmnInfoService.query(qo);
    }



    //上传文件
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String path = bpmnInfoService.upload(file);
        return AjaxResult.success("操作成功", path);
    }

    // 流程定义部署
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path,String info,String bpmnType) throws IOException {
        bpmnInfoService.deploy(path,info,bpmnType);
        return AjaxResult.success();
    }

    // 流程定义删除
    @RequiresPermissions("business:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {
        Integer delete = bpmnInfoService.delete(id);
        if (delete == null) {
            return AjaxResult.error("操作失败","请完成全部审核之后再删除");
        }
        return AjaxResult.success();
    }

    // 查看流程定义
    @RequestMapping("readResource")
    public void readResource(String deploymentId, String type, HttpServletResponse resp) throws IOException {
        bpmnInfoService.readResource(deploymentId,type,resp);
    }

}
